import 'package:sl_fast_dev/sl_fast_dev.dart';
import 'package:sl_fast_dev/src/dao/sl_decoration.dart';
import 'package:core_extension/core_extension.dart';

class SLNodeType {
  static const String SLNodeTypeNone = 'SLNodeTypeNone';

  static const String UIView = 'UIView';
  static const String UITableView = 'UITableView';
  static const String UITableViewCell = 'UITableViewCell';
  static const String UIScrollView = 'UIScrollView';

  static const String UIViewController = 'UIViewController';

  static const String UILabel = 'UILabel';
  static const String UITextField = 'UITextField';
  static const String UIButton = 'UIButton';
  static const String UIImageView = 'UIImageView';

  static const String RACCommand = 'RACCommand';

  static const String NSString = 'NSString';
}

enum SLNodeFor {
  SLNodeForUI,
  SLNodeForVM,
  SLNodeForVC,
}

class SLNode {
  SLNodeFor nodeFor;
  bool isRoot;

  final String name;
  final String type;
  final String text;
  final String desc;
  final String vm;
  final List<SLNode> children;
  final SLDecoration decoration;

  SLNode(
    this.name,
    this.type, {
    this.text,
    this.desc,
    this.vm,
    this.children,
    this.decoration,
  }) {
    nodeFor = SLNodeFor.SLNodeForUI;
  }

  bool hasChildren() =>
      (children != null && children.isNotEmpty) ? true : false;

  bool canBindRAC() {
    if (type == SLNodeType.UILabel) return true;
    if (type == SLNodeType.UIButton) return true;
    if (type == SLNodeType.UITextField) return true;
    return false;
  }

  bool isUITableView() => type == SLNodeType.UITableView ? true : false;
  bool isUITableViewCell() => type == SLNodeType.UITableViewCell ? true : false;
  bool isUIScrollView() => type == SLNodeType.UIScrollView ? true : false;
  bool isPlaceholder() => type == SLNodeType.SLNodeTypeNone ? true : false;
  bool isContainer() => (isUITableView() || isUIScrollView()) ? true : false;
  bool isCreateVM() => (vm != null && vm == '0') ? false : true;

  List<SLNode> getAllUITableViewCells() {
    var cells = <SLNode>[];
    if (isUITableView() && hasChildren()) {
      children.forEach((child) {
        if (child.isUITableViewCell()) cells.add(child);
      });
    }

    return cells;
  }

  List<SLNode> getAllUIScrollViewSubViews() {
    var cells = <SLNode>[];
    if (isUIScrollView() && hasChildren()) {
      children.forEach((child) {
        if (child.hasChildren()) cells.add(child);
      });
    }

    return cells;
  }

  List<SLNode> getAllChildrenCells() {
    var cells = <SLNode>[];

    if (!hasChildren()) return cells;

    children.forEach((child) {
      if (child.isUITableView() && child.hasChildren()) {
        child.children.forEach((cell) {
          if (cell.isUITableViewCell()) cells.add(cell);
        });
      }
    });

    return cells;
  }

  String property() => name + type.replaceFirst('UI', '');
  String nameString() => name + 'String';
  String nameList() => name + 'DataSource';
  String nameCMD() => name + 'Command';
  String clsName() {
    if (nodeFor == SLNodeFor.SLNodeForVM) return viewModel();
    if (nodeFor == SLNodeFor.SLNodeForVC) return vc();
    return cls();
  }

  String cls() => SLFastDevConfig().prefix + property().sl_capitalize();
  String viewModel() =>
      SLFastDevConfig().prefix +
      property().sl_capitalize() +
      (property().endsWith('View') ? 'Model' : 'ViewModel');

  String vc() =>
      SLFastDevConfig().prefix +
      property().sl_capitalize() +
      (property().endsWith('View') ? 'Controller' : 'ViewController');

  @override
  String toString() => 'name:$name,'
      'type:$type,'
      'text:$text,'
      'desc:$desc,'
      'vm:$vm';
  // 'children:$children'
}
